home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / amiga / asrc29k.lha / enet.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-08  |  2.8 KB  |  140 lines

  1. /* Stuff generic to all Ethernet controllers */
  2. #include <stdio.h>
  3. #include "global.h"
  4. #include "mbuf.h"
  5. #include "iface.h"
  6. #include "timer.h"
  7. #include "arp.h"
  8. #include "ip.h"
  9. #include "enet.h"
  10.  
  11. char Ether_bdcst[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  12.  
  13. /* Convert Ethernet header in host form to network mbuf */
  14. struct mbuf *
  15. htonether(ether,data)
  16. struct ether *ether;
  17. struct mbuf *data;
  18. {
  19.     struct mbuf *bp;
  20.     register char *cp;
  21.  
  22.     if((bp = pushdown(data,ETHERLEN)) == NULLBUF)
  23.         return NULLBUF;
  24.  
  25.     cp = bp->data;
  26.  
  27.     memcpy(cp,ether->dest,EADDR_LEN);
  28.     cp += EADDR_LEN;
  29.     memcpy(cp,ether->source,EADDR_LEN);
  30.     cp += EADDR_LEN;
  31.     put16(cp,ether->type);
  32.  
  33.     return bp;
  34. }
  35. /* Extract Ethernet header */
  36. int
  37. ntohether(ether,bpp)
  38. struct ether *ether;
  39. struct mbuf **bpp;
  40. {
  41.     pullup(bpp,ether->dest,EADDR_LEN);
  42.     pullup(bpp,ether->source,EADDR_LEN);
  43.     ether->type = pull16(bpp);
  44.     return ETHERLEN;
  45. }
  46.  
  47. /* Format an Ethernet address into a printable ascii string */
  48. char *
  49. pether(out,addr)
  50. char *out,*addr;
  51. {
  52.     sprintf(out,"%02x:%02x:%02x:%02x:%02x:%02x",
  53.      uchar(addr[0]),uchar(addr[1]),
  54.      uchar(addr[2]),uchar(addr[3]),
  55.      uchar(addr[4]),uchar(addr[5]));
  56.     return out;
  57. }
  58.  
  59. /* Convert an Ethernet address from Hex/ASCII to binary */
  60. int
  61. gether(out,cp)
  62. register char *out;
  63. register char *cp;
  64. {
  65.     register int i;
  66.  
  67.     for(i=6; i!=0; i--){
  68.         *out++ = htoi(cp);
  69.         if((cp = strchr(cp,':')) == NULLCHAR)    /* Find delimiter */
  70.             break;
  71.         cp++;            /* and skip over it */
  72.     }
  73.     return i;
  74. }
  75. /* Send an IP datagram on Ethernet */
  76. int
  77. enet_send(bp,iface,gateway,prec,del,tput,rel)
  78. struct mbuf *bp;    /* Buffer to send */
  79. struct iface *iface;    /* Pointer to interface control block */
  80. int32 gateway;        /* IP address of next hop */
  81. int prec;
  82. int del;
  83. int tput;
  84. int rel;
  85. {
  86.     char *egate;
  87.  
  88.     egate = res_arp(iface,ARP_ETHER,gateway,bp);
  89.     if(egate != NULLCHAR)
  90.         return (*iface->output)(iface,egate,iface->hwaddr,IP_TYPE,bp);
  91.     return 0;
  92. }
  93. /* Send a packet with Ethernet header */
  94. int
  95. enet_output(iface,dest,source,type,data)
  96. struct iface *iface;    /* Pointer to interface control block */
  97. char *dest;        /* Destination Ethernet address */
  98. char *source;        /* Source Ethernet address */
  99. int16 type;        /* Type field */
  100. struct mbuf *data;    /* Data field */
  101. {
  102.     struct ether ep;
  103.     struct mbuf *bp;
  104.  
  105.     memcpy(ep.dest,dest,EADDR_LEN);
  106.     memcpy(ep.source,source,EADDR_LEN);
  107.     ep.type = type;
  108.     if((bp = htonether(&ep,data)) == NULLBUF){
  109.         free_p(data);
  110.         return -1;
  111.     }
  112.     iface->lastsent = Clock;
  113.     iface->rawsndcnt++;
  114.     return (*iface->raw)(iface,bp);
  115. }
  116. /* Process incoming Ethernet packets. Shared by all ethernet drivers. */
  117. void
  118. eproc(iface,bp)
  119. struct iface *iface;
  120. struct mbuf *bp;
  121. {
  122.     struct ether hdr;
  123.  
  124.     /* Remove Ethernet header and kick packet upstairs */
  125.     ntohether(&hdr,&bp);
  126.  
  127.     switch(hdr.type){
  128.     case ARP_TYPE:
  129.         arp_input(iface,bp);
  130.         break;
  131.     case IP_TYPE:
  132.         ip_route(iface,bp,hdr.dest[0] & 1);
  133.         break;
  134.     default:
  135.         free_p(bp);
  136.         break;
  137.     }
  138. }
  139.  
  140.